package cn.yunhe.service.impl;

import cn.yunhe.dao.ExamDao;
import cn.yunhe.entity.Exam;
import cn.yunhe.entity.User;
import cn.yunhe.service.ExamService;
import cn.yunhe.util.DateUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("examService")
public class ExamServiceImpl implements ExamService {

    @Resource
    private ExamDao examDao;

    @Override
    public List<Exam> getAllExam(Exam exam) {
        return examDao.getAllExam(exam);
    }

    @Override
    public int getExamCount(Exam exam) {
        return examDao.getExamCount(exam);
    }

    @Override
    public Page getExamByPage(Map<String, Object> map) {
        updAllExamAudit();
        Page page = new Page();
        //设置 总条数
        int totalNum = examDao.getExamCount((Exam) map.get("exam"));
        page.setTotal(totalNum);
        // 获取页大小
        int pageSize = (int) map.get("pageSize");
        // 获取当前页
        int currPage = map.get("currPage") == null ? 1 : (int) map.get("currPage");
        //计算总页数
        int totalPage = (totalNum / pageSize) + (totalNum % pageSize == 0 ? 0 : 1);
        page.setPageNum(totalPage);
        //启用分页
        page = PageHelper.startPage(currPage, pageSize);
        examDao.getAllExam((Exam) map.get("exam"));
        return page;
    }

    @Override
    public int updExamAudit(Exam exam) {
        return examDao.updExamAudit(exam);
    }

    @Override
    public Exam getExamById(int id) {
        return examDao.getExamById(id);
    }

    @Override
    public int updExamInfo(Exam exam) {
        return examDao.updExamInfo(exam);
    }

    @Override
    public int delExam(int id) {
        return examDao.delExam(id);
    }

    @Override
    public int updAllExamAudit() {
        Exam exam = new Exam();
        byte b = 1;
        exam.setApproved(b);
        List<Exam>list = examDao.getAllExam(exam);
        Date date = new Date();
        int count = 0;
        for(Exam e : list){
            if(date.getTime() >= (e.getEndTime()).getTime()){
                count += examDao.updAllExamAudit(e.getId());
            }
        }
        return count;
    }

    @Override
    public int addExam(Exam exam) {
        return examDao.addExam(exam);
    }

    @Override
    public   Page getExamApplication(Map<String ,Object> map) {

        Page page = new Page();

        //获取总条数
        page.setTotal(examDao.getExamApplicationCount());
        //     System.out.println(tagDao.getTagListCountByPage(map));
        // 获取每页大小
        int pageSize = Integer.parseInt(String.valueOf(map.get("pageSize")));
        page.setPageSize(pageSize);

        // 获取当前页码
        int curPageNum = Integer.parseInt(String.valueOf(map.get("pageNum")));
//		page.setPageNum(curPageNum);
        // 计算出总页数
        page.setPageNum((int) (page.getTotal() / pageSize + (page.getTotal() % pageSize == 0 ? 0 : 1)));
        // 启用分页
        page = PageHelper.startPage(curPageNum, pageSize);
        // 查询列表
        List list =examDao.getExamApplication();
        return page;

    }

    @Override
    public int updExamApplication(int id) {


        return examDao.updExamApplication(id);
    }

    @Override
    public int delApplication(int id) {

        return examDao.delApplication(id);
    }

    /**
     * 获取用户考试活跃信息
     * @param cond 封装分页数据pageSize,pageNum,考试名称examName
     * @return
     * 郭瀚钰
     */
    @Override
    public Page getUserExamInfo(Map cond) {
        //创建一个新的怕个对象
        Page page = new Page();
        //计算总记录数
        page.setTotal(examDao.getUserExamInfoCount(cond));
        //获取页大小
        int pageSize = (int) cond.get("pageSize");
        page.setPageSize(pageSize);
        //获取当前页
        int currPageNum = (int) cond.get("pageNum");
        page.setPageNum(currPageNum);
        //计算总页数
        page.setPageNum((int)(page.getTotal()/pageSize+(page.getTotal()%pageSize==0? 0:1)));
        //启用分页
        page = PageHelper.startPage(currPageNum,pageSize);
        List userExamInfoList = examDao.getUserExamInfo(cond);
        return page;
    }

    public ExamDao getExamDao() {
        return examDao;
    }

    public void setExamDao(ExamDao examDao) {
        this.examDao = examDao;
    }
}
